package com.vim.modules.web.model;

import com.vim.common.model.base.BaseEntity;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * UI菜单列表
 */
public class UIMenu extends BaseEntity{

    /** 菜单ID */
    private String id;

    /** 菜单名称 */
    private String title;

    /** 菜单图标 */
    private String icon;

    /** 是否展开 */
    private boolean spread;

    /** 地址 */
    private String url;

    /** 子菜单 */
    List<UIMenu> children;

    public List<UIMenu> build(List<SysMenu> sysMenuList){
        Map<String, UIMenu> uiMenuMap = new LinkedHashMap<>();
        List<UIMenu> uiMenuList = new ArrayList<>();
        //一级菜单
        int i = 0;
        for(SysMenu sysMenu:sysMenuList){
            if(sysMenu.getMenuLevel() == 1){
                UIMenu uiMenu = new UIMenu();
                uiMenu.setId(sysMenu.getMenuId());
                uiMenu.setTitle(sysMenu.getMenuName());
                if(i == 0){
                    uiMenu.setSpread(true);
                }else{
                    uiMenu.setSpread(false);
                }
                if(StringUtils.isNoneBlank(sysMenu.getMenuIcon())){
                    uiMenu.setIcon(sysMenu.getMenuIcon());
                }else{
                    uiMenu.setIcon("");
                }
                uiMenu.setChildren(new ArrayList<>());
                uiMenuMap.put(sysMenu.getMenuId(), uiMenu);
            }
            i++;
        }
        //二级菜单
        for(SysMenu sysMenu:sysMenuList){
            if(sysMenu.getMenuLevel() > 1){
                UIMenu uiMenu = uiMenuMap.get(sysMenu.getMenuParentId());
                if(null != uiMenu){
                    UIMenu sub = new UIMenu();
                    sub.setId(sysMenu.getMenuId());
                    sub.setTitle(sysMenu.getMenuName());
                    if(StringUtils.isNoneBlank(sysMenu.getMenuIcon())){
                        sub.setIcon(sysMenu.getMenuIcon());
                    }else{
                        sub.setIcon("");
                    }
                    sub.setUrl(sysMenu.getMenuUrl());
                    uiMenu.getChildren().add(sub);
                }
            }
        }
        for(String str:uiMenuMap.keySet()){
            uiMenuList.add(uiMenuMap.get(str));
        }
        return uiMenuList;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public boolean isSpread() {
        return spread;
    }

    public void setSpread(boolean spread) {
        this.spread = spread;
    }

    public List<UIMenu> getChildren() {
        return children;
    }

    public void setChildren(List<UIMenu> children) {
        this.children = children;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}
